package com.c.ctgproject.ui.collect

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.CarCanHandlerAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityCarCanHandlerListBinding
import com.c.ctgproject.entity.guang.BatteryCanHandlerEntity
import com.c.ctgproject.entity.guang.CarCanHandlerEntity
import com.c.ctgproject.event.PositionsEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2021/9/9
 *描述:可拆解车辆列表 页面 新建拆解单 页面
 */
class CarCanHandlerListActivity : BaseActivityV2(true), CarCanHandlerAdapter.OnCheckBoxClickedListener {
    private val mBinding by lazy { ActivityCarCanHandlerListBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search: String = ""

    private var mList = ArrayList<CarCanHandlerEntity.DataBean.ListBean>()
    private val mAdapter by lazy { CarCanHandlerAdapter(this, mList,this) }


    @Subscribe
    fun event(type: PositionsEvent) {
        val removeList = ArrayList<CarCanHandlerEntity.DataBean.ListBean>()
        type.list.forEach { a ->
            mList.forEach { b ->
                if (a == b.carId) {
                    removeList.add(b)
                }
            }
        }
        removeList.forEach {
            mList.remove(it)
        }
        mAdapter.notifyDataSetChanged()
    }

    override fun event() {
        getData()
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)

    }

    var searchMine = false
    override fun click() {
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "未拆解" -> {
                    searchMine = false
                    page = 1
                    getData()
                    mBinding.mBottomRL.visibility = View.VISIBLE

                }
                "我的" -> {
                    searchMine = true
                    page = 1
                    getData()
                    mBinding.mBottomRL.visibility = View.GONE

                }
            }
        }
        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
        //去拆解
        mBinding.mConfirmBTN.setOnClickListener {
            val data = CarCanHandlerEntity.DataBean()
            val list = ArrayList<CarCanHandlerEntity.DataBean.ListBean>()
            mList.forEach { listBean ->
                if (listBean.select) {
                    list.add(listBean)
                }
            }

            if (list.isEmpty()) {
                showToast("请至少选择一辆车进行拆解")
            } else {
                data.list = list
                //去拆解
                startActivity<CarHandlerActivity>(MyConstants.DATA to getGson().toJson(data))
            }

        }
    }

    private fun getData() {
        mBinding.mConfirmBTN.text="去拆解"
        if (searchMine) {
            //流程类型 1-预录入 2-登记入库 3-过磅 4-初检 5-预处理 6-新建拆解 7-完成拆解 8-毁型拍照
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.myCreateHandlerOrder(search,  page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.myHandlerCar(search, 6, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的车辆列表:$it")
                if (page == 1) mList.clear()
                val data = getGson().fromJson(it, CarCanHandlerEntity::class.java).data
                data?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mCarRV.handlerPosition(page)
                }
            })
        } else {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.carCanHandlerList(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("可拆列表:$it")
                val data = getGson().fromJson(it, CarCanHandlerEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mCarRV.handlerPosition(page)
                }
            })
        }
    }

    override fun OnItemCheckBoxClicked() {
        val list = ArrayList<CarCanHandlerEntity.DataBean.ListBean>()
        mList.forEach { listBean ->
            if (listBean.select) {
                list.add(listBean)
            }
        }
        if(list.isEmpty()){
            mBinding.mConfirmBTN.text="去拆解"
        }else{
            mBinding.mConfirmBTN.text="去拆解(${list.size})"
        }
    }


}